<?php

namespace App\Http\Resources\Passengers;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class PassengerResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $res               = parent::toArray($request);
        $res['is_invite']  = ($res['invite_user_id'] ?? false) ? 1 : 0;
        $split             = "·";
        $names             = explode($split, $res['truename']);
        $res['first_name'] = $names[1] ?? '';          // 姓
        $res['last_name']  = $names[0] ?? '';          // 名
        unset(
            $res['invite_user_id'],
            $res['gender'],
            $res['delete_state'],
            $res['user_id'],
            $res['birth_day']
        );
        return $res;
    }
}
